package com.huike.business.service.impl;


import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.utils.DictUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
@RequiredArgsConstructor
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    private final TbBusinessMapper tbBusinessMapper;
    private final SysDictDataMapper dictDataMapper;

    @Override
    public void insertRecord(BusinessTrackVo businessTrackVo) {
        businessTrackVo.setCreateBy(SecurityUtils.getUsername());
        businessTrackVo.setUpdateBy(SecurityUtils.getUsername());
        businessTrackVo.setUpdateTime(LocalDateTime.now());
        tbBusinessMapper.insertRecord(businessTrackVo);
    }

    @Override
    public List<BusinessTrackVo> selectRecord(Long id) {
        List<BusinessTrackVo> recordList = tbBusinessMapper.selectRecord(id);
        //缓存中获取
        List<SysDictData> dictDatas = DictUtils.getDictCache("communication_point");
        if (StringUtils.isEmpty(dictDatas))
        {
            dictDatas = dictDataMapper.selectDictDataByType("communication_point");
        }
        Map<String, String> map = new HashMap<>();
        dictDatas.stream().map(dict ->{
            String dictValue = dict.getDictValue();
            String dictLabel = dict.getDictLabel();
            map.put(dictValue, dictLabel);
            return null;
        });
        recordList.stream().map(record ->{
            String[] keys = record.getKeyItems().split(",");
            for (String key : keys){
                record.getKeys().add(map.get(key));
            }
            return null;
        });
        return recordList;
    }
}
